package simpleGui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.util.Vector;

import processing.core.PGraphics;

public class GuiClickButton extends GuiComponent {
	boolean push;
	Vector<ActionListener>actionListener=new Vector<ActionListener>();

	GuiClickButton(Gui gui, int x, int y, int w, int h) {
		this.setDimensions(x, y, w, h);
		this.gui=gui;
		gui.parent.addMouseListener(this);
	}
	public GuiClickButton(Gui gui, int x, int y, int w, int h, String label) {
		this(gui,x,y,w,h);
		this.label=label;
	}
	
	public void display(PGraphics g) {
		
		g.stroke(gui.getStrokeColor());
		g.strokeWeight(1);
		super.display(g);
		if (push){
			g.fill(gui.getFillColorActivated());
			g.rect(x,y,w,h);
		}
		if (label!=null){
		g.fill(gui.getStrokeColor());
		g.text(label,x+gui.getPadding(),y+h-gui.getPadding());
		}
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		super.mousePressed(arg0);
		if (this.contains(arg0.getX(),arg0.getY())){
			push=true;
		}
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		super.mouseReleased(arg0);
		if (this.contains(arg0.getX(),arg0.getY())){
			if (push==true){
				ActionEvent event=new ActionEvent(this,0,this.getLabel());
				for (int i=0;i<actionListener.size();i++){
					actionListener.get(i).actionPerformed(event);
				}
			}
		}
		push=false;
	}
	public void addActionListener(ActionListener l){
		this.actionListener.add(l);
	}
}
